<template>
  <div>
    <!-- <div bg-white overflow-hidden text-slate-600>
      <div m-20rpx px-16rpx py-10rpx rounded-md text-28rpx bg-green-50 v-if="success" class="tips success">
        {{ success }}
      </div>
      <div m-20rpx px-16rpx py-10rpx rounded-md text-28rpx bg-orange-50 v-if="error" class="tips error">
        {{ error }}
      </div>
    </div> -->

    <div p-32rpx bg-white v-if="bindWechat">
      <div text-40rpx>提现金额</div>
      <nut-input :input-style="mobileInputStyle" :border="false" py-20rpx v-model="applyMoney">
        <template #left>
          <span text-70rpx>￥</span>
        </template>
        <template #right>
          <div @click="checkAll" text-24rpx flex align-end flex-col justify-end>
            <div text-right text-main-color>全部提现</div>
            <div mt-10rpx>
              可提现金额
              <span>{{ walletNum }}</span>
              元
            </div>
          </div>
        </template>
      </nut-input>
      <div text-24rpx mt-20rpx>提现金额最低为{{ minPrice }}元</div>
      <!-- 是否为第三方登录 -->
      <!-- <nut-form ref="aliFormRef" :model-value="aliForm" v-if="type === 'ALI'">
        <nut-form-item
          label="真实姓名"
          prop="realName"
          label-align="left"
          required
          :rules="[{ required: true, message: '请输入真实姓名' }]"
          label-width="110">
          <nut-input v-model="aliForm.realName" placeholder="请输入您的真实姓名" :border="false" />
        </nut-form-item>
        <nut-form-item
          label="第三方登录账号"
          prop="connectNumber"
          label-align="left"
          required
          :rules="[{ required: true, message: '请输入第三方登录账号' }]"
          label-width="110">
          <nut-input v-model="aliForm.connectNumber" placeholder="请输入您的第三方登录账号" :border="false" />
        </nut-form-item>
      </nut-form> -->

      <submit @click="confirm" />
    </div>
    <div v-else mx-20rpx mt-60rpx>
      <nut-button block type="primary" @click="bindWechatSkip">绑定微信</nut-button>
    </div>
    <div bg-white mt-20rpx>
      <div p-32rpx text-40rpx>提现信息</div>
      <nut-form ref="ruleForm" :model-value="formData" :rules="rules">
        <nut-form-item label="姓名" prop="name">
          <nut-input v-model="formData.name"  placeholder="请输入姓名" type="text" />
        </nut-form-item>
        <nut-form-item label="身份证号" prop="idCard">
          <nut-input v-model="formData.idCard"  placeholder="请输入身份证号" type="text" />
        </nut-form-item>
        <nut-form-item label="所属银行" prop="bank">
          <nut-input v-model="formData.bank"  placeholder="请输入所属银行" type="text" />
        </nut-form-item>
        <nut-form-item label="支行名称" prop="subBankName">
          <nut-input v-model="formData.subBankName"  placeholder="请输入支行名称" type="text" />
        </nut-form-item>
        <nut-form-item label="银行卡号" prop="bankNo">
          <nut-input v-model="formData.bankNo"  placeholder="请输入银行卡号" type="text" />
        </nut-form-item>
      </nut-form>
    </div>
  </div>
</template>

<script setup lang="ts">
import { withdrawalSettingVO } from '@/api/common'
import { getUserWallet, withdrawalApply } from '@/api/member'
import submit from '@/components/sheet-btn/index.vue'
import storage from '@/utils/storage'
import { onLoad } from '@dcloudio/uni-app'
import api from '@/config/api'
import { openIdBind } from '@/api/login'
import { money } from '@/utils/RegExp'
import type { FormInst } from 'nutui-uniapp'

const mobileInputStyle = {
  height: '140rpx',
  fontSize: '70rpx'
}
const routerVal = ref<any>({})
const error = ref<string>('') //错误消息
const success = ref<string>('') //成功绑定消息
const accessToken = ref<any>('') //用户当前token
const applyMoney = ref<number>(0)
const walletNum = ref<any>(0)
const minPrice = ref<any>(0)
const maxPrice = ref<any>(200)
const type = ref<any>('WECHAT')
const bindWechat = ref<any>(true)
const aliFormRef = ref<any>(null)
const aliForm = ref<any>({
  realName: '', // 真实姓名
  connectNumber: '' // 第三方登录账号
})

const ruleForm = ref<FormInst | null>(null)

const formData = ref({
  name: '',
  idCard: '',
  bank: '',
  subBankName: '',
  bankNo: ''
})
const rules = {
  name: [{ required: true, message: '请输入姓名' }],
  idCard: [{ required: true, message: '请输入身份证号' }],
  bank: [{ required: true, message: '请输入所属银行' }],
  subBankName: [{ required: true, message: '请输入支行名称' }],
  bankNo: [{ required: true, message: '请输入银行卡号' }]
}


//可提现金额
function checkAll() {
  applyMoney.value = walletNum.value
}
// 绑定微信
function bindWechatSkip() {
  let buyer = api.buyer
  // #ifdef H5
  window.open(buyer + `/passport/connect/connect/bind/web/WECHAT?token=${accessToken.value}`, 'blank')
  // #endif
  // #ifndef H5
  let openId = ''
  uni.login({
    provider: 'weixin',
    success: function (res) {
      openId = res.authResult.openid
    },
    fail(e) {
      console.error(e)
      uni.showToast({ title: '第三方登录暂不可用！', icon: 'none', duration: 3000 })
    },
    complete(e) {
      openIdBind({ unionID: openId, type: 'WECHAT_APP_OPEN_ID' }).then((res) => {
        if (res.data.success) {
          uni.showToast({ title: '绑定成功！', icon: 'none', duration: 3000 })
          bindWechat.value = true
        } else {
          uni.showToast({ title: res.data.message, icon: 'none', duration: 3000 })
        }
      })
    }
  })
  // #endif
}

// 点击提交按钮
async function confirm() {
  if (!money.test(applyMoney.value) || applyMoney.value == 0) {
    uni.showToast({
      title: '请输入正确的金额',
      icon: 'none'
    })
    return
  }
  if (Number(applyMoney.value) < minPrice.value) {
    uni.showToast({
      title: `提现金额不能小于${minPrice.value}元`,
      icon: 'none'
    })
    return
  }
  ruleForm.value?.validate().then(({ valid, errors }) => {
    if (valid) {
      let params = { applyMoney: applyMoney.value, ...formData.value } as any
      let isResult = true as boolean
      // 是否为第三方登录
      // if (type.value === 'ALI') {
      //   isResult = false
      //   aliFormRef.value.validate().then(({ valid }: any) => {
      //     if (valid) {
      //       params.connectNumber = aliForm.value.connectNumber
      //       params.realName = aliForm.value.realName
      //       isResult = true
      //     } else {
      //       isResult = false
      //     }
      //   })
      // }
      if (isResult) {
        uni.showLoading({ title: '提现中...' })
        withdrawalApply(params).then((res) => {
          if (res.data.success) {
            uni.showToast({ title: '提现成功!', duration: 2000, icon: 'none', mask: true })
            setTimeout(() => {
              uni.navigateBack({ delta: 1 })
            }, 1000)
          }
        }).catch(() => {
          setTimeout(() => {
            uni.hideLoading()
          }, 2500)
          
        })
      }
    }
  })
}

async function init() {
  Promise.all([getUserWallet(), withdrawalSettingVO()]).then((res) => {
    // 余额
    if (res[0].data.success) {
      walletNum.value = res[0].data.result ? res[0].data.result.memberWallet : 0
    }
    // if (res[1].data.success) {
    //   minPrice.value = res[1].data.result.minPrice
    //   maxPrice.value = res[1].data.result.maxPrice
    //   bindWechat.value = res[1].data.result.bindWechat
    //   type.value = res[1].data.result.type
    // }
  })
}

onLoad((options: any) => {
  routerVal.value = options
})

onMounted(() => {
  init()
  accessToken.value = storage.getAccessToken()
  // error.value = routerVal.value.error
  // success.value = routerVal.value.success
})
</script>

<style scoped></style>
